import 'package:json_annotation/json_annotation.dart';
import 'package:sjx/repository/model/task_process_step_model.dart';

part 'task_publish_process_model.g.dart';

@JsonSerializable()
class TaskPublishProcessModel {
  @JsonKey(name: 'submitCount', defaultValue: 0)
  final int submitCount;

  @JsonKey(name: 'viewCount', defaultValue: 0)
  final int viewCount;

  @JsonKey(name: 'taskStatus', defaultValue: '')
  final String taskStatus;

  @JsonKey(name: 'taskNo', defaultValue: '')
  final String taskNo;

  @JsonKey(name: 'taskTitle', defaultValue: '')
  final String taskTitle;

  @JsonKey(name: 'openView', defaultValue: '')
  final String openView;

  @JsonKey(name: 'rewardAmountDisplay', defaultValue: '')
  final String rewardAmountDisplay;

  @JsonKey(name: 'leftBidTimeDisplay', defaultValue: '')
  final String leftBidTimeDisplay;

  @JsonKey(name: 'endTimeDescription', defaultValue: '')
  final String endTimeDescription;

  @JsonKey(name: 'taskAllRewardAmountDisplay', defaultValue: '')
  final String taskAllRewardAmountDisplay;

  @JsonKey(name: 'historySteps', defaultValue: [])
  final List<TaskProcessStepModel> historySteps;

  TaskPublishProcessModel({
    required this.submitCount,
    required this.viewCount,
    required this.taskStatus,
    required this.taskNo,
    required this.taskTitle,
    required this.openView,
    required this.rewardAmountDisplay,
    required this.leftBidTimeDisplay,
    required this.endTimeDescription,
    required this.taskAllRewardAmountDisplay,
    required this.historySteps,
  });

  factory TaskPublishProcessModel.fromJson(Map<String, dynamic> json) {
    return _$TaskPublishProcessModelFromJson(json).._processHistorySteps();
  }

  Map<String, dynamic> toJson() => _$TaskPublishProcessModelToJson(this);

  /// 处理历史步骤数据，应用拒绝状态检测
  void _processHistorySteps() {
    for (var i = 0; i < historySteps.length; i++) {
      final step = historySteps[i];
      historySteps[i] = step.withRefuseDetection();
    }
  }

  /// 获取待审核数量（从任务状态描述中提取）
  int get pendingReviewCount {
    final regExp = RegExp(r'待审核：(\d+)');
    final match = regExp.firstMatch(taskStatus);
    if (match != null && match.groupCount >= 1) {
      return int.tryParse(match.group(1)!) ?? 0;
    }

    // 从历史步骤中查找
    for (final step in historySteps) {
      final stepMatch = regExp.firstMatch(step.stepComment);
      if (stepMatch != null && stepMatch.groupCount >= 1) {
        return int.tryParse(stepMatch.group(1)!) ?? 0;
      }
    }

    return 0;
  }

  /// 判断任务是否已过期
  bool get isExpired {
    return endTimeDescription.contains('任务已到期') ||
        endTimeDescription.contains('已过期') ||
        taskStatus.contains('已结束');
  }

  /// 判断任务是否需要审核
  bool get needsReview {
    return pendingReviewCount > 0 &&
        (taskStatus.contains('等待结束') ||
            taskStatus.contains('进行中'));
  }

  /// 获取任务状态颜色
  String get statusColor {
    if (isExpired) return '#999999'; // 灰色-已过期
    if (needsReview) return '#FF6B35'; // 橙色-需要审核
    if (taskStatus.contains('通过') || taskStatus.contains('已完成')) {
      return '#52C41A'; // 绿色-已完成
    }
    return '#1890FF'; // 蓝色-进行中
  }

  @override
  String toString() {
    return 'TaskDetailModel{submitCount: $submitCount, viewCount: $viewCount, taskStatus: $taskStatus, taskNo: $taskNo, taskTitle: $taskTitle, pendingReviewCount: $pendingReviewCount}';
  }

}